/*
 * 文件名：RoleInfoTransactionImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-30
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.reservation.bookinfo.transaction.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.BeanUtils;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.Constants;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.dao.CommonDao;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.customer.custmanager.dao.CustManagerDao;
import com.wootide.cms.service.customer.custmanager.pojo.Customer;
import com.wootide.cms.service.facility.room.roominfo.dao.RoomInfoDao;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomInfo;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomPeriod;
import com.wootide.cms.service.facility.room.table.dao.TableNoInfoDao;
import com.wootide.cms.service.facility.room.table.pojo.TableNoInfo;
import com.wootide.cms.service.facility.room.table.pojo.TablePeriod;
import com.wootide.cms.service.reservation.bookinfo.dao.BookOrderInfoDao;
import com.wootide.cms.service.reservation.bookinfo.pojo.BookOrderInfo;
import com.wootide.cms.service.reservation.bookinfo.transaction.BookOrderInfoTransaction;
import com.wootide.cms.service.reservation.orderby.dao.OrderByInfoDao;
import com.wootide.cms.service.reservation.orderby.pojo.OrderByInfo;
import com.wootide.cms.service.reservation.payment.dao.PaymentMethodsInfoDao;

/**
 * 用户事物层实现层
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 * @author ozm
 * @version 2013-1-30
 * @see BookOrderInfoTransactionImp
 * @since 2013-1-30上午9:44:19
 */
public class BookOrderInfoTransactionImp implements BookOrderInfoTransaction
{
   

    // 注入运行日志
    private RunLog runLog;
    
    //公共数据访问层实例
    private CommonDao commonDao;
    
    //数据访问层实例
    private BookOrderInfoDao bookOrderInfoDao;
    

    //房间数据访问层
    private RoomInfoDao roomInfoDao;
    
    //预订方式数据访问层
    private OrderByInfoDao  orderByInfoDao;
    //支付方式数据访问层
    private PaymentMethodsInfoDao paymentMethodsInfoDao;
    
    //客户信息数据访问层
    private CustManagerDao custManagerDao;
    
    //注入桌次访问层
    private TableNoInfoDao tableNoInfoDao;
    
    /**
     * 
     * Description: 分页查询
     * Implement: <br>
     * @param page 分页参数
     * @param queryConditon 查询条件
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午1:23:32
     */
    public QueryResults queryForPages(Page page, QueryCondition queryConditon)
    {
        
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        QueryResults queryResults = commonDao.queryForPages(page, queryConditon);
        return queryResults;
    }
    
    /**
     * 
     * Description: 添加一条客户预订信息
     * Implement: <br>
     * @param queryConditon 要添加的客户预订信息
     * @see
     * @author ozm2013-1-29下午1:26:06
     */
    public void addOneBookOrder(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        BookOrderInfo bookOrderInfo = (BookOrderInfo)queryConditon.getConditionValueList().get(2);
        
        // 检查唯一性
        Integer num = bookOrderInfoDao.findNumByBookOrderName(bookOrderInfo.getOrderNum());
        
        //记录已经存在
        if (num > 0)
        {
            throw new CMSRuntimeException("BookOrderInfo Existed!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFOEXIT);
        }
       
        //绑定客户信息
        bookOrderInfo.setCustomerInfo(getOldCustomerInfo(bookOrderInfo));
        
        commonDao.addOneObject(bookOrderInfo);
        
//        BookOrderInfo bookOrderInfo = (BookOrderInfo)queryConditon.getConditionValueList().get(2);
        // 检查唯一性
        BookOrderInfo oldBookOrderInfo = bookOrderInfoDao.findOneBookOrderByOrderNum(bookOrderInfo.getOrderNum());
        
        //绑定时段信息
        if (null != queryConditon.getConditionValueList().get(0))
        {
            List roomPeriodList = roomInfoDao.queryPeriodsByIdList(new ArrayList((Set<Integer>)(queryConditon.getConditionValueList().get(0))),Constants.PeriodType.ROOM_PERIOD);
            
            for (Object obj : roomPeriodList)
            {
                RoomPeriod  rp = (RoomPeriod)obj;
                rp.setStatus(Constants.PeriodStatus.ORDERED);
                rp.setBookOrderInfo(bookOrderInfo);
            }
            
            oldBookOrderInfo.setRoomPeriods(new TreeSet(roomPeriodList));
        }
        
        if (null != queryConditon.getConditionValueList().get(1))
        {
            List tablePeriodList = roomInfoDao.queryPeriodsByIdList(new ArrayList((Set<Integer>)(queryConditon.getConditionValueList().get(1))),Constants.PeriodType.TABLE_PERIOD);
           
            for (Object obj : tablePeriodList)
            {
                TablePeriod  tp = (TablePeriod)obj;
                tp.setStatus(Constants.PeriodStatus.ORDERED);
                tp.setBookOrderInfo(bookOrderInfo);
            }
            
            oldBookOrderInfo.setTablePeriods(new TreeSet(tablePeriodList));
            
        }
        
        //记录订单添加时间
        bookOrderInfo.setDeneratedDate(CommonUtil.getCurrentTime());
      
    }
    

    
    /**
     * 
     * Description: 修改一条客户预订信息
     * Implement: <br>
     * @param bookOrderInfo 要修该的客户预订信息
     * @see
     * @return bookOrderInfo 修改的客户预订信息对象
     * @author ozm2013-1-29下午1:27:37
     */
    public BookOrderInfo modOneBookOrder(BookOrderInfo bookOrderInfo)
    {
        // 打印方法入口参数
        runLog.debug("bookOrderInfo=" + bookOrderInfo);
        BookOrderInfo oldBookOrderInfo = bookOrderInfoDao.findOneBookOrderById(bookOrderInfo.getInnerIndex());
        
        //判断要修改的记录是否存在
        if (null == oldBookOrderInfo)
        {
            throw new CMSRuntimeException("BookOrderInfo not Exist!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFONOTEXIT);
        }
        
        //获取原始数据副本
        BookOrderInfo oldBookOrderInfoCopy = new BookOrderInfo();
        BeanUtils.copyProperties(oldBookOrderInfo, oldBookOrderInfoCopy);
        
        //修改用户信息
       
        Customer  customerCondition = null;    
        
        if (null == oldBookOrderInfo.getCustomerInfo())
        {
           //非会员修改
            if (null != bookOrderInfo.getCustomerInfo().getCardNo() )
            {
                //卡号被修改
                customerCondition = new Customer();   
                customerCondition.setCardNo(bookOrderInfo.getCustomerInfo().getCardNo());
            }
            else
            {
              //预订人和号码被修改
                customerCondition = new Customer();   
                customerCondition.setCustName(bookOrderInfo.getCustomerName());
                customerCondition.setTel(bookOrderInfo.getTel());
            }
            
        }
        else
        {
            //会员预订修改
            if (oldBookOrderInfo.getCustomerInfo().getCardNo() != bookOrderInfo.getCustomerInfo().getCardNo() )
            {
                //卡号被修改
                customerCondition = new Customer();   
                customerCondition.setCardNo(bookOrderInfo.getCustomerInfo().getCardNo());
            }
            else if (oldBookOrderInfo.getCustomerName() != bookOrderInfo.getCustomerName() || oldBookOrderInfo.getTel() != bookOrderInfo.getTel())
            {
                //预订人和号码被修改
                customerCondition = new Customer();   
                customerCondition.setCustName(bookOrderInfo.getCustomerName());
                customerCondition.setTel(bookOrderInfo.getTel());
            }
            
            
        }
      
        if (null != customerCondition)
        {
            Customer  customerInfo = custManagerDao.findOneCustomerByNameAndPhoneOrCardNo(customerCondition);
            
            if (null != customerInfo)
            {
                oldBookOrderInfo.setCustomerName(customerInfo.getCustName());
                oldBookOrderInfo.setTel(customerInfo.getTel());
            }
            
            oldBookOrderInfo.setCustomerInfo(customerInfo);
        }
       
        OrderByInfo oldOrderByInfo = orderByInfoDao.findOneOrderByByOrderById(bookOrderInfo.getOrderByInfo().getInnerIndex());
        oldBookOrderInfo.setOrderByInfo(oldOrderByInfo);
        oldBookOrderInfo.setCustomerName(bookOrderInfo.getCustomerName());
        oldBookOrderInfo.setTel(bookOrderInfo.getTel());
        
        
        oldBookOrderInfo.setPaymentMethodsInfo(bookOrderInfo.getPaymentMethodsInfo());
        oldBookOrderInfo.setPayStatus(bookOrderInfo.getPayStatus());
        return oldBookOrderInfoCopy;
    }
    
    
    /**
     * 修改一房间的时段信息
     * Description: <br>
     * Implement: <br>
     * @param bookOrderInfo 订单信息
     * @param innerIndexs 修改后的时段索引
     * @return
     * @see
     * @return BookOrderInfo
     * @author ozm2013-4-12下午5:35:04
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public BookOrderInfo modOneOrderRoomPeriod(BookOrderInfo bookOrderInfo,
            Integer[] innerIndexs)
    {
        // 打印方法入口参数
        runLog.debug("bookOrderInfo=" + bookOrderInfo);
        runLog.debug("innerIndexs=" + innerIndexs);
        BookOrderInfo oldBookOrderInfo = bookOrderInfoDao.findOneBookOrderById(bookOrderInfo.getInnerIndex());
        //判断要修改的记录是否存在
        if (null == oldBookOrderInfo)
        {
            throw new CMSRuntimeException("BookOrderInfo not Exist!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFONOTEXIT);
        }
        
        //取消绑定
        List removeRoomPeriods = roomInfoDao.queryRoomPeriodsNotInIdListByOrderId(bookOrderInfo.getInnerIndex(), innerIndexs);
        for (Object obj : removeRoomPeriods)
        {
            RoomPeriod  rp = (RoomPeriod)obj;
            rp.setStatus(Constants.PeriodStatus.FREE);
            rp.setBookOrderInfo(null);
        }
        
        oldBookOrderInfo.getRoomPeriods().removeAll(new TreeSet(removeRoomPeriods));
        
        //需要绑定的
        List updateRoomPeriods = roomInfoDao.queryRoomPeriodsInIdListByOrderId(bookOrderInfo.getInnerIndex(), innerIndexs);
        
        for (Object obj : updateRoomPeriods)
        {
            
            RoomPeriod  rp = (RoomPeriod)obj;
           
            if (null == rp.getBookOrderInfo())
            {
                rp.setStatus(Constants.PeriodStatus.ORDERED);
                rp.setBookOrderInfo(bookOrderInfo);
            }
            
        }
        
        oldBookOrderInfo.getRoomPeriods().addAll(new TreeSet(updateRoomPeriods));
        
       
        
       return null;
        
    }
    
    
    /**
     * 修改一桌次的时段信息
     * Description: <br>
     * Implement: <br>
     * @param bookOrderInfo 订单信息
     * @param innerIndexs 修改后的时段索引
     * @return
     * @see
     * @return BookOrderInfo 订单信息
     * @author ozm2013-4-12下午5:35:04
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public BookOrderInfo modOneOrderTablePeriod(BookOrderInfo bookOrderInfo,
            Integer[] innerIndexs)
    {
        // 打印方法入口参数
        runLog.debug("bookOrderInfo=" + bookOrderInfo);
        runLog.debug("innerIndexs=" + innerIndexs);
        BookOrderInfo oldBookOrderInfo = bookOrderInfoDao.findOneBookOrderById(bookOrderInfo.getInnerIndex());
        //判断要修改的记录是否存在
        if (null == oldBookOrderInfo)
        {
            throw new CMSRuntimeException("BookOrderInfo not Exist!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFONOTEXIT);
        }
        
        //取消绑定
        List removeTablePeriods = tableNoInfoDao.queryTablePeriodsNotInIdListByOrderId(bookOrderInfo.getInnerIndex(), innerIndexs);
        for (Object obj : removeTablePeriods)
        {
            TablePeriod  rp = (TablePeriod)obj;
            rp.setStatus(Constants.PeriodStatus.FREE);
            rp.setBookOrderInfo(null);
        }
        
        oldBookOrderInfo.getTablePeriods().removeAll(new TreeSet(removeTablePeriods));
        
        //需要绑定的
        List updateTablePeriods = tableNoInfoDao.queryTablePeriodsInIdListByOrderId(bookOrderInfo.getInnerIndex(), innerIndexs);
        
        for (Object obj : updateTablePeriods)
        {
            
            TablePeriod  rp = (TablePeriod)obj;
           
            if (null == rp.getBookOrderInfo())
            {
                rp.setStatus(Constants.PeriodStatus.ORDERED);
                rp.setBookOrderInfo(bookOrderInfo);
            }
            
        }
        
        oldBookOrderInfo.getTablePeriods().addAll(new TreeSet(updateTablePeriods));
        
       
        
       return null;
    }
    
    /**
     * 
     * Description: 删除客户预订信息
     * Implement: <br>
     * @param innerIndexs 要删除的客户预订信息id
     * @return
     * @see
     * @return BookOrderInfo 要删除的客户预订信息
     * @author ozm2013-1-29下午4:17:56
     */
    public BookOrderInfo delOneBookOrder(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        BookOrderInfo oldBookOrderInfo = bookOrderInfoDao.findOneBookOrderById(innerIndex);
        //判断要删除的记录是否存在
        if (null == oldBookOrderInfo)
        {
            throw new CMSRuntimeException("BookOrderInfo not Exist!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFONOTEXIT);
        }
        
        for (Object obj : oldBookOrderInfo.getRoomPeriods())
        {
            RoomPeriod roomPeriod = (RoomPeriod)obj;
            
            roomPeriod.setStatus(Constants.PeriodStatus.FREE);
            roomPeriod.setBookOrderInfo(null);
            
        }
        
        for (Object obj : oldBookOrderInfo.getTablePeriods())
        {
            TablePeriod tablePeriod = (TablePeriod)obj;
            
            tablePeriod.setStatus(Constants.PeriodStatus.FREE);
            tablePeriod.setBookOrderInfo(null);
            
        }
        
        commonDao.delOneObject(oldBookOrderInfo);
        //        BeanUtils.copyProperties(oldUserInfo, oldUserInfoCopy);
        return oldBookOrderInfo;
    }
    
    
    /**
     * 取消已经预订的时段
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 条件
     * @return
     * @see
     * @return BookOrderInfo 更新后的订单对象
     * @author ozm2013-4-16下午1:28:34
     */
    public BookOrderInfo delOrderPeriods(QueryCondition queryConditon)
    {
        runLog.debug("queryConditon=" + queryConditon);
        BookOrderInfo bookOrderInfo = (BookOrderInfo)queryConditon.getConditionValueList().get(0);
        BookOrderInfo oldBookOrderInfo = bookOrderInfoDao.findOneBookOrderById(bookOrderInfo.getInnerIndex());
        //判断要删除的记录是否存在
        if (null == oldBookOrderInfo)
        {
            throw new CMSRuntimeException("BookOrderInfo not Exist!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFONOTEXIT);
        }
        
        //获取原始数据副本
        BookOrderInfo oldBookOrderInfoCopy = new BookOrderInfo();
        BeanUtils.copyProperties(oldBookOrderInfo, oldBookOrderInfoCopy);
        //房间时段
        if (null != queryConditon.getConditionValueList().get(1))
        {
            for (Object obj : oldBookOrderInfo.getRoomPeriods())
            {
                RoomPeriod roomPeriod = (RoomPeriod)obj;
                
                if (queryConditon.getConditionValueList().get(1).equals(roomPeriod.getRoomInfo().getRoomNum()))
                {
                    roomPeriod.setStatus(Constants.PeriodStatus.FREE);
                    roomPeriod.setBookOrderInfo(null);
                    oldBookOrderInfo.getRoomPeriods().remove(roomPeriod);
                }
            }
        }
        
        
      //房间时段
        if (null != queryConditon.getConditionValueList().get(2))
        {
            for (Object obj : oldBookOrderInfo.getTablePeriods())
            {
                TablePeriod tablePeriod = (TablePeriod)obj;
                
                if (queryConditon.getConditionValueList().get(2).equals(tablePeriod.getTableNoInfo().getTableNo()))
                {
                    tablePeriod.setStatus(Constants.PeriodStatus.FREE);
                    tablePeriod.setBookOrderInfo(null);
                    oldBookOrderInfo.getTablePeriods().remove(tablePeriod);
                }
            }
        }
        
        return oldBookOrderInfoCopy;
    }
    
    /**
     * 
     * Description: 通过索引获取客户预订信息
     * Implement: <br>
     * @param innerIndex 指定的客户预订信息ID
     * @see
     * @return QueryResults 要查询的客户预订信息
     * @author ozm2013-1-29下午3:55:17
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public QueryResults viewOneBookOrder(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        //获取全部预订方式和支付方式
        List paymentMethodsList = paymentMethodsInfoDao.findAllPaymentMethods();
        //获取支付方式列表
        List orderByList = orderByInfoDao.findAllOrderBys();
        //获取订单信息
        BookOrderInfo bookOrderInfo = bookOrderInfoDao.findOneBookOrderById(innerIndex);
        
        List orderNoTableRooms = null;
        List orderTableRooms = null;
        
        if (null != bookOrderInfo)
        {
            //获取订单预订的房间列表（不包含房间）
            orderNoTableRooms = roomInfoDao.queryOrderRoomsByOrderId(bookOrderInfo.getInnerIndex());
            
            for (Object obj : orderNoTableRooms)
            {
                RoomInfo roomInfo = (RoomInfo)obj;
                 //其他类型房间
                List roomPeriodList = roomInfoDao.queryRoomPeriodByOrderIdAndRoomId(bookOrderInfo.getInnerIndex(), roomInfo.getInnerIndex());
                roomInfo.setRoomPeriods(new TreeSet(roomPeriodList));
                //获取时段字符串
                roomInfo.setPeriodStr(CommonUtil.getPeriodsStr(roomInfo.getRoomPeriods(),Constants.PeriodType.ROOM_PERIOD));    
            }
            
            //订单包含房间（有桌次）
            orderTableRooms  = tableNoInfoDao.queryRoomsByOrderId(bookOrderInfo.getInnerIndex());
            
            for (Object obj : orderTableRooms)
            {
                RoomInfo roomInfo = (RoomInfo)obj;
                //获取桌次信息列表
                List tableNoInfoList = tableNoInfoDao.queryTableNoInfosByOrderIdAndRoomId(bookOrderInfo.getInnerIndex(), roomInfo.getInnerIndex());
                for (Object o : tableNoInfoList)
                {
                    TableNoInfo tableNoInfo = (TableNoInfo)o;
                    //时段列表
                    List tablePeriodList = tableNoInfoDao.queryTablePeriodByOrderIdAndTableId(bookOrderInfo.getInnerIndex(), tableNoInfo.getInnerIndex());
                    
                    tableNoInfo.setTablePeriods(new TreeSet(tablePeriodList));
                    //获取时段字符串
                    tableNoInfo.setPeriodStr(CommonUtil.getPeriodsStr(tableNoInfo.getTablePeriods(),Constants.PeriodType.TABLE_PERIOD));
                }
                
                roomInfo.setTableNoInfos(new TreeSet(tableNoInfoList)); 
            }   
        }
        else
        {
            throw new CMSRuntimeException("BookOrderInfo not Exist!",
                    ErrorCodes.Reservation.BookOrderInfo.BOOKORDERINFONOTEXIT);
        }
        
      
        QueryResults queryResults  = new QueryResults();
        List resultsList = new ArrayList();
        List roomList = new ArrayList();
        
        if (orderNoTableRooms.size()  > 0)
        {
            roomList.addAll(orderNoTableRooms);
        }
        
        if (orderTableRooms.size() > 0)
        {
            roomList.addAll(orderTableRooms);
        }
        
       
        
       
        resultsList.add(orderByList);
        resultsList.add(paymentMethodsList);
        resultsList.add(roomList);
        resultsList.add(bookOrderInfo);
        queryResults.setResults(resultsList);
        return queryResults;
    }
    
    /**
     * 
     * Description: <br>
     * Implement: <br>
     * @param queryConditon
     * @return
     * @see
     * @return QueryResults
     * @author ozm2013-3-15下午2:29:21
     */   
    @SuppressWarnings("rawtypes")
    public QueryResults queryBookableRoomPeriod(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        List  bookableRoomPeriods = roomInfoDao.queryBookableRoomPeriods(queryConditon);
//        if (!CommonUtil.collectionIsNull(bookableRoomPeriods))
//        {
//            RoomPeriod roomPeriod = (RoomPeriod)bookableRoomPeriods.get(0);
//            roomPeriod.getRoomInfo().getRoomTypeInfo().getRoomTypeName();
//        }
        QueryResults queryResults  = new QueryResults();
        queryResults.setResults(bookableRoomPeriods);
        return queryResults;
    }
    
    /**
     * 查询可预订房间信息
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 查询条件
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-3-15下午2:03:01
     */
    @SuppressWarnings("rawtypes")
    public QueryResults queryBookableRooms(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        //获取房间记录集合
        List  bookableRooms= roomInfoDao.findBookableRoomsByConditions(queryConditon);
//        if (!CommonUtil.collectionIsNull(bookableRooms))
//        {
//            RoomInfo roomInfo = (RoomInfo)bookableRooms.get(0);
//            roomInfo.getRoomTypeInfo().getRoomTypeName();
//        }
        
//        List bookableRoomPeriods = null;
//        
//        if (!CommonUtil.collectionIsNull(bookableRooms))
//        {
//           
//            bookableRoomPeriods =  roomInfoDao.queryBookableRoomPeriods(queryConditon);
//            
//        }
        
        QueryResults queryResults  = new QueryResults();
        queryResults.setResults(bookableRooms);
//        queryResults.setExResults(bookableRoomPeriods);
        
        return queryResults;
    }

    
    /**
     * 查询可预订桌次信息
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 查询条件
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-3-15下午2:03:01
     */
    @SuppressWarnings("rawtypes")
    public QueryResults queryBookableTableNos(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        
        List  bookableTableNos = roomInfoDao.queryBookableTableNos(queryConditon);
//        if (!CommonUtil.collectionIsNull(bookableTableNos))
//        {
//            TableNoInfo TableNoInfo = (TableNoInfo)bookableTableNos.get(0);
//            TableNoInfo.getRoomInfo().getRoomTypeInfo().getRoomTypeName();
//        }
        QueryResults queryResults  = new QueryResults();
        queryResults.setResults(bookableTableNos);
        return queryResults;
    }
    
    
    /**
     * 查询可预订桌次可预订时段
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 查询条件
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-3-15下午2:03:01
     */
    @SuppressWarnings("rawtypes")
    public QueryResults queryBookableTablePeriods(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        List  bookableTablePeriods = roomInfoDao.queryBookableTablePeriods(queryConditon);
//        if (!CommonUtil.collectionIsNull(bookableTablePeriods))
//        {
//            TablePeriod tablePeriod = (TablePeriod)bookableTablePeriods.get(0);
//            tablePeriod.getTableNoInfo().getRoomInfo().getRoomTypeInfo().getRoomTypeName();
//        }
        QueryResults queryResults  = new QueryResults();
        queryResults.setResults(bookableTablePeriods);
        return queryResults;
    }
    
    /**
     * 查询全部预订预览信息
     * Description: <br>
     * Implement: <br>
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-3-26下午6:47:23
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public QueryResults queryBookingPreviewInfo(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        //获取所有支付方式
        List paymentMethodsList = paymentMethodsInfoDao.findAllPaymentMethods();
        //获取所有预订方式
        List orderByList = orderByInfoDao.findAllOrderBys();
        //获取欲预订房间信息列表
        List<RoomInfo> roomList = roomInfoDao.queryBookingPreviewRoomsByPeriods(queryConditon);
        
        //欲生成订单信息
        BookOrderInfo bookOrderInfo = (BookOrderInfo)queryConditon.getConditionValueList().get(2);

        //绑定客户信息
        getOldCustomerInfo(bookOrderInfo);
        
        for (RoomInfo roomInfo : roomList)
        {
            queryConditon.getConditionValueList().add(roomInfo.getInnerIndex());
            
            if(Constants.RooomType.CHABA.equals(roomInfo.getRoomTypeInfo().getRoomTypeId()) 
                    ||Constants.RooomType.CHESS_ROOM.equals(roomInfo.getRoomTypeInfo().getRoomTypeId()))
            {
                // 棋牌室或者茶吧
                List<TableNoInfo> tableNoInfoList = roomInfoDao.queryBookingPreviewTableNosByPeriods(queryConditon);
                
                for(TableNoInfo tableNoInfo : tableNoInfoList)
                {
                    runLog.debug("tableNoInfo=" + tableNoInfo);
                    queryConditon.getConditionValueList().add(tableNoInfo.getInnerIndex());
                    List tablePeriodList = roomInfoDao.queryBookingPreviewTablePeriodsByIds(queryConditon);
                    tableNoInfo.setTablePeriods(new TreeSet(tablePeriodList));
                    queryConditon.getConditionValueList().remove(tableNoInfo.getInnerIndex());
                }
                
                roomInfo.setTableNoInfos(new TreeSet(tableNoInfoList));
            }
            else
            {
                //其他类型房间
                List roomPeriodList = roomInfoDao.queryBookingPreviewRoomPeriodsByIds(queryConditon);
                roomInfo.setRoomPeriods(new TreeSet(roomPeriodList));

                
            }
            
            queryConditon.getConditionValueList().remove(roomInfo.getInnerIndex());
        }
        
        
       
        
        QueryResults queryResults  = new QueryResults();
        List resultsList = new ArrayList();
        resultsList.add(orderByList);
        resultsList.add(paymentMethodsList);
        resultsList.add(roomList);
        resultsList.add(bookOrderInfo);
        queryResults.setResults(resultsList);
//        queryResults.setExResults(paymentMethodsList);
        return queryResults;
    }

    
    /**
     * 查询支付方式和预订方式
     * Description: <br>
     * Implement: <br>
     * @return
     * @see
     * @return QueryResults 结果集合
     * @author ozm2013-3-28下午7:52:57
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public QueryResults queryBookableOrderBysAndPayments()
    {
        runLog.debug("queryBookableOrderBysAndPayments");
        List paymentMethodsList = paymentMethodsInfoDao.findAllPaymentMethods();
        List orderByList = orderByInfoDao.findAllOrderBys();
        QueryResults queryResults  = new QueryResults();
        List resultsList = new ArrayList();
        resultsList.add(orderByList);
        resultsList.add(paymentMethodsList);
        queryResults.setResults(resultsList);
        return queryResults;
    }
    
    /**
     * 查询订单预览时间段信息
     * Description: <br>
     * Implement: <br>
     * @return
     * @see
     * @return QueryResults
     * @author ozm2013-3-30下午11:56:05
     */
    @SuppressWarnings("rawtypes")
    public QueryResults queryPreviewPeriods(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        List  bookableTableNos = roomInfoDao.queryPreviewPeriods(queryConditon);
        QueryResults queryResults  = new QueryResults();
        queryResults.setResults(bookableTableNos);
        return queryResults;
    }
    
    /**
     * 通过索引获取房间预订时段
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 房间索引和订单索引
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-4-10下午4:04:04
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public QueryResults queryOrderRoomPeriods(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        List<?>  orderRoomPeriodList = roomInfoDao.queryRoomPeriodByOrderIdAndRoomId((Integer) queryConditon.getConditionValueList().get(0)
                ,(Integer) queryConditon.getConditionValueList().get(1));
        
        RoomInfo roomInfo = roomInfoDao.findOneRoomInfoById((Integer) queryConditon.getConditionValueList().get(1));
        QueryResults queryResults  = new QueryResults();
        List resultsList = new ArrayList();
        resultsList.add(orderRoomPeriodList);
        resultsList.add(roomInfo);
        queryResults.setResults(resultsList);
        return queryResults;
    }
    
    /**
     * 通过索引获取索引预订时段
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 索引索引和订单索引
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-4-10下午4:04:04
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public QueryResults queryOrderTablePeriods(QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        List<?>  orderTablePeriodList = tableNoInfoDao.queryTablePeriodByOrderIdAndTableId((Integer) queryConditon.getConditionValueList().get(0)
                ,(Integer) queryConditon.getConditionValueList().get(1));
        
        TableNoInfo tableNoInfo = tableNoInfoDao.findOneTableNoInfoById((Integer) queryConditon.getConditionValueList().get(1));
        QueryResults queryResults  = new QueryResults();
        List resultsList = new ArrayList();
        resultsList.add(orderTablePeriodList);
        resultsList.add(tableNoInfo);
        queryResults.setResults(resultsList);
        return queryResults;
    }
    
    /**
     * 通过起止时间和房间桌次信息查询时段信息
     * Description: <br>
     * Implement: <br>
     * @param queryConditon 查询条件（起止时间、房间ID、桌次ID）
     * @return
     * @see
     * @return QueryResults 结果集
     * @author ozm2013-4-11下午5:40:50
     */
 
    @SuppressWarnings({"rawtypes", "unchecked"})
    public QueryResults queryPeriods(QueryCondition queryConditon)
    {
        List<?>  periodList = null;
        List resultsList = new ArrayList();
        
        if (null != queryConditon.getConditionValueList().get(2))
        {
            //房间时间
            periodList = roomInfoDao.queryPeriodsByRoomIdAndStartTimeAndEndTime(queryConditon.getConditionValueList().get(0)
                    , queryConditon.getConditionValueList().get(1),queryConditon.getConditionValueList().get(2));
            RoomInfo roomInfo = roomInfoDao.findOneRoomInfoById((Integer) queryConditon.getConditionValueList().get(2));
            resultsList.add(periodList);
            resultsList.add(roomInfo);

        }
        else
        {
            //桌次时段
            periodList = tableNoInfoDao.queryPeriodsByTableIdAndStartTimeAndEndTime(queryConditon.getConditionValueList().get(0)
                    , queryConditon.getConditionValueList().get(1), queryConditon.getConditionValueList().get(3));
            TableNoInfo tableNoInfo = tableNoInfoDao.findOneTableNoInfoById((Integer)queryConditon.getConditionValueList().get(3));
            resultsList.add(periodList);
            resultsList.add(tableNoInfo);
        }
        
        QueryResults queryResults  = new QueryResults();
        queryResults.setResults(resultsList);
        return queryResults;
    }
    
    /**
     * 获取已经存在的客户信息
     * Description: <br>
     * Implement: <br>
     * @param bookOrderInfo 订单信息
     * @return
     * @see
     * @return Customer
     * @author ozm2013-4-7下午2:02:22
     */
    private Customer getOldCustomerInfo(BookOrderInfo bookOrderInfo)
    {
        //客户信息
        Customer customerInfo = new Customer();
        
        if (null != bookOrderInfo.getCustomerInfo()
                &&  null != bookOrderInfo.getCustomerInfo().getCardNo())
        {
            customerInfo.setCardNo(bookOrderInfo.getCustomerInfo().getCardNo());
        }
        else
        {  
            customerInfo.setCustName(bookOrderInfo.getCustomerName());
            customerInfo.setTel(bookOrderInfo.getTel());
        }
        
        //获取客户信息
        Customer oldCustomerInfo = custManagerDao.findOneCustomerByNameAndPhoneOrCardNo(customerInfo);
        if (null != oldCustomerInfo)
        {
            bookOrderInfo.setBookerName(oldCustomerInfo.getCustName());
            bookOrderInfo.setTel(oldCustomerInfo.getTel());
            bookOrderInfo.setCustomerInfo(oldCustomerInfo);
            bookOrderInfo.setDiscount(oldCustomerInfo.getClientLevelId().getDiscount());
        }
        else
        {
            //设置默认折扣
            bookOrderInfo.setDiscount(1.0);
        }
        
        return oldCustomerInfo;
    }
    
   
    
   
    public RunLog getRunLog()
    {
        return runLog;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public CommonDao getCommonDao()
    {
        return commonDao;
    }

    public void setCommonDao(CommonDao commonDao)
    {
        this.commonDao = commonDao;
    }


    public BookOrderInfoDao getBookOrderInfoDao()
    {
        return bookOrderInfoDao;
    }

    public void setBookOrderInfoDao(BookOrderInfoDao bookOrderInfoDao)
    {
        this.bookOrderInfoDao = bookOrderInfoDao;
    }
    
    public RoomInfoDao getRoomInfoDao()
    {
        return roomInfoDao;
    }

    public void setRoomInfoDao(RoomInfoDao roomInfoDao)
    {
        this.roomInfoDao = roomInfoDao;
    }

    public OrderByInfoDao getOrderByInfoDao()
    {
        return orderByInfoDao;
    }

    public void setOrderByInfoDao(OrderByInfoDao orderByInfoDao)
    {
        this.orderByInfoDao = orderByInfoDao;
    }

    public PaymentMethodsInfoDao getPaymentMethodsInfoDao()
    {
        return paymentMethodsInfoDao;
    }

    public void setPaymentMethodsInfoDao(PaymentMethodsInfoDao paymentMethodsInfoDao)
    {
        this.paymentMethodsInfoDao = paymentMethodsInfoDao;
    }

    public CustManagerDao getCustManagerDao()
    {
        return custManagerDao;
    }

    public void setCustManagerDao(CustManagerDao custManagerDao)
    {
        this.custManagerDao = custManagerDao;
    }

    public TableNoInfoDao getTableNoInfoDao()
    {
        return tableNoInfoDao;
    }

    public void setTableNoInfoDao(TableNoInfoDao tableNoInfoDao)
    {
        this.tableNoInfoDao = tableNoInfoDao;
    }

    

    


}
